# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: AlexWMF <alexxwmf@gmail.com>
# Contributor: Zach Bacon <11doctorwhocanada@gmail.com>

_realname=libtorrent-rasterbar
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.2.7
pkgrel=1
pkgdesc="libtorrent is a feature complete C++ bittorrent implementation focusing on efficiency and scalability (mingw-w64)"
arch=('any')
license=('BSD')
url="https://libtorrent.org/"
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=(staticlibs strip)
source=("https://github.com/arvidn/libtorrent/releases/download/libtorrent_${pkgver//./_}/${_realname}-${pkgver}.tar.gz")
sha256sums=('bc00069e65c0825cbe1eee5cdd26f94fcd9a621c4e7f791810b12fab64192f00')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DBUILD_SHARED_LIBS=ON \
    -Dbuild_tests=OFF \
    -Dbuild_examples=OFF \
    ../${_realname}-${pkgver}

  make

  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir ${srcdir}/build-${MINGW_CHOST}-static && cd ${srcdir}/build-${MINGW_CHOST}-static

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -Dbuild_tests=OFF \
    -Dbuild_examples=OFF \
    -DBUILD_SHARED_LIBS=OFF \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}-static
  make DESTDIR=${pkgdir} install

  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install
  
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
